﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MechWars
{
    public class Player
    {
        public Vector2 position;
        public Vector2 velocity;
        public Vector2 gravity = new Vector2(0, -0.07f);
        public Vector2 control;
        public Vector2 forward;
        public bool flipped;
        public bool canJump;
        public Polygon collsionPoly = new Polygon();
        public JMesh mesh = new JMesh();
        public void flipGravity()
        {
            flipped = !flipped;
            gravity = -gravity;
        }
        public Player(Vector2 pos,bool flip,Color color)
        {
            flipped = flip;
            if(flip)
            {
                gravity = -gravity;
            }
            mesh.effect.DiffuseColor = color.ToVector3();
            collsionPoly.AddVertex(new Vector2(-1,-2));
            collsionPoly.AddVertex(new Vector2(1,-2));
            collsionPoly.AddVertex(new Vector2(1,2));
            collsionPoly.AddVertex(new Vector2(-1,2));
            mesh.AddCubeBoid(new Vector3(1f, 2f, 0.5f), new Vector3(-1f, -2f, -0.5f));

        }
        public void Jump(bool doubleJump)
        {
            if (canJump || doubleJump)
            {
                if (flipped)
                {
                    velocity.Y -= 1.5f;
                }
                else
                {
                    velocity.Y += 1.5f;
                }
            }
            canJump = false;
            
        }
        public void applyPhiscalCorrections(PhysicalInfo info)
        {
            position = info.newPosition;
            velocity = info.newVelocity;
            if (flipped)
            {
                forward = info.floorDir * new Vector2(-1, 1);
            }
            else
            {
                forward = info.floorDir;
            }
            canJump = canJump || info.floor;
        }
        public void Update(float movement)
        {
            velocity += gravity;
            control += (movement * 1f * forward);
            velocity *= 0.999f;
            control *= 0.5f;
            position += velocity + control;
            collsionPoly.origin = position;
            mesh.effect.World = Matrix.CreateTranslation(new Vector3(position,0));
            mesh.Update();
        }
    }
}
